﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Pinballs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!Page.IsPostBack)
        {
            sel_tour.DataSource = Tournament.getAvailableTournaments();
            sel_tour.DataTextField = "Tournament_Name";
            sel_tour.DataValueField = "Pk_Tournament";
            sel_tour.DataBind();

            if (sel_tour.Items.Count > 0)
            {

                sel_number.DataSource = Machine.GetMachinesByTournament(Int32.Parse(sel_tour.SelectedValue));
                sel_number.DataTextField = "Machine_Name";
                sel_number.DataValueField = "Pk_Machine";
                sel_number.DataBind();
                if (sel_number.Items.Count > 0)
                {
                    Machine oMachine = new Machine(Int32.Parse(sel_number.SelectedValue));
                    txt_acron.Value = oMachine.Machine_Acron;
                    txt_name.Value = oMachine.Machine_Name;
                }
            }

        }
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Pinballs));
    }
    [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.Read)]
    public int MakePin(string p_Name,string p_Acron,int p_Number,int p_Tournament)
    {
        try
        {

            Machine oMachine = new Machine(p_Name, p_Acron, Convert.ToByte(p_Number));
            return Machine.InsertMachine(oMachine, p_Tournament);

        }
        catch
        {
            return -1;
        }
    }


    protected void sel_number_SelectedIndexChanged(object sender, EventArgs e)
    {
        Machine oMachine = new Machine(Int32.Parse(sel_number.SelectedValue));
        txt_acron.Value = oMachine.Machine_Acron;
        txt_name.Value = oMachine.Machine_Name;  
    }
    protected void Button1_ServerClick(object sender, EventArgs e)
    {
        try
        {
            //borramos pinball
            int Pk_Machine = Int32.Parse(sel_number.SelectedValue);
            int Pk_Tournament = Int32.Parse(sel_tour.SelectedValue);

            Boolean Result = Machine.DeleteMachine(Pk_Machine);
            if (Result == true)
            {
                if (removequalify.Checked) { 
                    //Delete from qualify                    
                    QualifyRounds.DeleteMachineInQualify(Pk_Tournament, Pk_Machine);                   
                }
                if (removeplayoffs.Checked) {
                    PlayoffRounds.DeleteMachineInPlayoffs(Pk_Tournament, Pk_Machine); 
                }  

                okmsg.Style["display"] = "block";
                sel_number.DataSource = Machine.GetMachinesByTournament(Int32.Parse(sel_tour.SelectedValue));
                sel_number.DataTextField = "Machine_Name";
                sel_number.DataValueField = "Pk_Machine";
                sel_number.DataBind();
                if (sel_number.Items.Count > 0)
                {
                    Machine oMachine = new Machine(Int32.Parse(sel_number.SelectedValue));
                    txt_acron.Value = oMachine.Machine_Acron;
                    txt_name.Value = oMachine.Machine_Name;
                }
                else
                {
                    txt_acron.Value = "";
                    txt_name.Value = "";
                }
            }
            else
            {
                badmsg.Style["display"] = "block";
            }
        }
        catch
        {
            badmsg.Style["display"] = "block";
        }
       
    }
    protected void sel_tour_SelectedIndexChanged(object sender, EventArgs e)
    {
        
            sel_number.DataSource = Machine.GetMachinesByTournament(Int32.Parse(sel_tour.SelectedValue));
            sel_number.DataTextField = "Machine_Name";
            sel_number.DataValueField = "Pk_Machine";
            sel_number.DataBind();
            if (sel_number.Items.Count > 0)
            {
            Machine oMachine = new Machine(Int32.Parse(sel_number.SelectedValue));
            txt_acron.Value = oMachine.Machine_Acron;
            txt_name.Value = oMachine.Machine_Name;
        }
        else {
            txt_acron.Value = "";
            txt_name.Value = "";
        }
    }
}
